from fastapi import FastAPI

import aioredis

from fastapi.middleware.cors import CORSMiddleware
from apps.user.userView import router

app = FastAPI()
app.include_router(router, prefix="/api/v1.0")

'''
跨域🥝
'''

#  配置允许域名
origins = [
	"http://127.0.0.1:8080",
	"http://localhost:8080",
]
#  配置允许域名列表、允许方法、请求头、cookie等
app.add_middleware(
	CORSMiddleware,
	allow_origins=origins,
	allow_credentials=True,
	allow_methods=["*"],
	allow_headers=["*"],
)

'''
redis🥝
'''


# 创建会话上下文管理器（可选）
async def get_redis_connection():
	redis = await aioredis.create_redis_pool("redis://127.0.0.1:6379/0")
	return redis


@app.on_event("startup")
async def startup():
	app.state.redis = await get_redis_connection()


@app.on_event("shutdown")
async def shutdown():
	await app.state.redis.close()
	await app.state.redis.wait_closed()

